﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace TestRegularExression
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_Match_Click(object sender, EventArgs e)
        {
            textBox_MatchResult.Text = string.Empty;
            if(textBox_SourceString.TextLength<1)
            {
                //MessageBox.Show("待匹配字串为空");
                //return;
                textBox_SourceString.Text = "abbdeef3";
            }
            if(textBox_PattenString.TextLength<1)
            {
                //MessageBox.Show("匹配模式为空");
                //return;
                textBox_PattenString.Text = @"(?<=.{2}).{2}";
            }
            try
            {
                foreach (Match m in Regex.Matches(textBox_SourceString.Text, textBox_PattenString.Text))
                {
                    textBox_MatchResult.Text += m.Index.ToString() + " " + m.Value + "\r\n";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void button_Replace_Click(object sender, EventArgs e)
        {
            textBox_ReplaceResult.Text = string.Empty;
            if (textBox_SourceString.TextLength < 1)
            {
                //MessageBox.Show("待匹配字串为空");
                //return;
                textBox_SourceString.Text = "abbdeef3";
            }
            if (textBox_PattenString.TextLength < 1)
            {
                //MessageBox.Show("匹配模式为空");
                //return;
                textBox_PattenString.Text = @"(?<=.{2}).{2}";
            }
            if(textBox_ReplaceParam.TextLength<1)
            {
                textBox_ReplaceParam.Text = @"_$0";
            }
            Regex reg = new Regex(textBox_PattenString.Text);
            textBox_ReplaceResult.Text = reg.Replace(textBox_SourceString.Text, textBox_ReplaceParam.Text);
        }
    }
}
